<?php

/**
 * StaticPageController is the default controller for this application
 */
class StaticPageController extends BrowseControllerBase {
	/*
	 * UserPageDataService Object
	 */
	public $userPageDataService;
	
	/**
	 * Constructor of index
	 */
	public function __construct() {
		parent::__construct ();
		
		//Check if User is logged in
		$authentication = new Authentication ();
		if ($authentication->getCurrentUserId () == null) {
			// Go on to login page
			$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/';
			CommonUtils::redirect ( $url );
			return - 1;
		}
		
		// Init UserPageDataService
		$this->userPageDataService = new UserPageDataService ();
	}
	
	/**
	 * The "index" action is the default action for all controllers -- the
	 * landing page of the site.
	 *
	 * @return void
	 *
	 */
	public function indexAction() {
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Browse/UserPage/static_page_list.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Browse/MainLayout.tpl' );
	}
	
	/*
	 * Edit Static page
	 */
	public function editAction() {
		
		// Get id of page
		$id = $this->getPost ( 'id' );
		if (empty ( $id )) {
			$id = $this->getQuery ( 'id' );
		}
		
		$static_page_url = "";
		$static_page_text = "";
		$title_text = "";
		
		$type = $id . "_page";
		
		switch ($id) {
			case "home" :
				$static_page_url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/StaticPage/edit/id/home';
				$static_page_text = "HOME PAGE PANEL";
				$title_text = "Edit Homepage Area";
				break;
			case "aboutus" :
				$static_page_url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/StaticPage/edit/id/aboutus';
				$static_page_text = "ABOUT US PAGE PANEL";
				$title_text = "Edit About Us Area";
				break;
			case "contact" :
				$static_page_url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/StaticPage/edit/id/contact';
				$static_page_text = "CONTACT DETAILS PANEL";
				$title_text = "Edit Contact Details Area";
				break;
		}
		
		$authentication = new Authentication ();
		// Get Page Content
		$pageEntity = $this->userPageDataService->getByUserAndType ( $authentication->getCurrentUserId (), $type );
		
		if (empty ( $pageEntity )) {
			if ($type == "home_page") {
				//Create new one page
				$pageDefaultName = "Default Home Page";
				$pageEntity = $this->userPageDataService->beforeCreate ();
				$pageEntity->ownerId = $authentication->getCurrentUserId ();
				$pageEntity->title = $pageDefaultName;
				$pageEntity->name = $pageDefaultName;
				$pageEntity->content = $pageDefaultName;
				$pageEntity->metaDescription = $pageDefaultName;
				$pageEntity->metaKeywords = $pageDefaultName;
				$pageEntity->type = PAGE_HOME;
				$pageEntity->headTitle = $pageDefaultName;
				$pageEntity->status = 1;
				$this->userPageDataService->create ( $pageEntity );
			}
			
			if ($type == "contact_page") {
				$pageDefaultName = "Default Contact Page";
				$pageEntity = $this->userPageDataService->beforeCreate ();
				$pageEntity->ownerId = $authentication->getCurrentUserId ();
				$pageEntity->title = $pageDefaultName;
				$pageEntity->name = $pageDefaultName;
				$pageEntity->content = $pageDefaultName;
				$pageEntity->metaDescription = $pageDefaultName;
				$pageEntity->metaKeywords = $pageDefaultName;
				$pageEntity->headTitle = $pageDefaultName;
				$pageEntity->status = 1;
				$pageEntity->type = PAGE_CONTACT;
				$this->userPageDataService->create ( $pageEntity );
			}
			
			if ($type == "aboutus_page") {
				$pageDefaultName = "Default About Us Page";
				$pageEntity = $this->userPageDataService->beforeCreate ();
				$pageEntity->ownerId = $authentication->getCurrentUserId ();
				$pageEntity->title = $pageDefaultName;
				$pageEntity->name = $pageDefaultName;
				$pageEntity->content = $pageDefaultName;
				$pageEntity->metaDescription = $pageDefaultName;
				$pageEntity->metaKeywords = $pageDefaultName;
				$pageEntity->headTitle = $pageDefaultName;
				$pageEntity->status = 1;
				$pageEntity->type = PAGE_ABOUTUS;
				$this->userPageDataService->create ( $pageEntity );
			}
		}
		
		// Get action method and check it
		$actionForm = $this->getPost ( 'actionForm', '' );
		
		// Only Normal User and Admin can update this content
		if (! empty ( $actionForm ) && $actionForm == 'submit' && $this->isDemoMode == false) {
			
			// get content
			$content = $this->getPost ( 'content', "" );
			
			// remove magic quotes
			if (get_magic_quotes_gpc ()) {
				$content = stripslashes ( $content );
			}
			
			$content = trim ( $content );
			$content = htmlentities ( $content, ENT_QUOTES );
			
			// head title
			$headTitle = $this->getPost ( 'headTitle', "" );
			
			// Get description field
			$metaDescription = $this->getPost ( 'metaDescription', "" );
			
			// keywords
			$metaKeywords = $this->getPost ( 'metaKeywords', "" );
			
			// Main Title Page
			$title = $this->getPost ( 'title', "" );
			
			$pageEntity->content = $content;
			$pageEntity->headTitle = $headTitle;
			$pageEntity->metaDescription = $metaDescription;
			$pageEntity->metaKeywords = $metaKeywords;
			$pageEntity->title = $title;
			
			// check
			if (! empty ( $content )) {
				try {
					$res = $this->userPageDataService->update ( $pageEntity );
					
					$isError = false;
					
					// Show a message
					$error = 'The record has been updated successfully';
				} catch ( Exception $ex ) {
					
					// Ignore to process this case. Only show a message				
					$error = 'The item cannot create successfully. Error Code = ' . $ex->getCode ();
					$isError = true;
				}
			} else {
				$isError = true;
				
				$error = 'Please fill in all required fields';
			}
		}
		
		$this->smarty->assign ( 'headTitle', $pageEntity->headTitle );
		$this->smarty->assign ( 'metaKeywords', $pageEntity->metaKeywords );
		$this->smarty->assign ( 'metaDescription', $pageEntity->metaDescription );
		$this->smarty->assign ( 'title', $pageEntity->title );
		$this->smarty->assign ( 'content', $pageEntity->content );
		$this->smarty->assign ( 'id', $id );
		
		$this->smarty->assign ( 'static_page_url', $static_page_url );
		$this->smarty->assign ( 'static_page_text', $static_page_text );
		$this->smarty->assign ( 'title_text', $title_text );
		
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Browse/UserPage/edit_static_page.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Browse/MainLayout.tpl' );
	}
}
